using DG.Tweening;
using UnityEngine;
using UnityEngine.UI;

public class DisplayAllLives : GenericDialog
{
	private enum DisplayStyle
	{
		normal = 0,
		wildcard = 1,
		lowValue = 2
	}

	public Button button;

	public Image background;

	public Text label;

	public Image infinity;

	[Space(10f)]
	public Image icon;

	public Image imageMinusOne;

	[Space(10f)]
	public int value = 888;

	private int displayedValue = -1;

	private bool syncWithEconomy = true;

	private DisplayStyle currentDisplayStyle;

	[Space(10f)]
	public AnimationResourceFlow animationFlow;

	public Ease animationEase = Ease.Linear;

	public float animationInitialDelay = 0.4f;

	public float animationMoveDuration = 0.2f;

	public float animationInterval = 0.07f;

	public float animationValueDuration = 0.25f;

	private Sequence sequenceMinusOne;

	public override void Start()
	{
		base.Start();
		if (syncWithEconomy && Economy.hasInstance)
		{
			value = Economy.lives;
		}
		displayedValue = value;
		label.text = displayedValue.ToString();
		Economy.LivesChangedEvent += OnLivesChanged;
	}

	private void OnDestroy()
	{
		Economy.LivesChangedEvent -= OnLivesChanged;
	}

	private void OnLivesChanged(int deltaLives)
	{
		if (syncWithEconomy)
		{
			value = Economy.lives;
		}
	}

	private void Update()
	{
		TryUpdateDisplayValue();
		TryUpdateDisplayStyle();
	}

	private void TryUpdateDisplayValue()
	{
		if (displayedValue != value)
		{
			if (value == displayedValue - 1)
			{
				ShowMinusOne();
			}
			displayedValue = value;
			label.text = displayedValue.ToString();
			DoAnimationPulseScale(label, 1.5f, 1f, animationValueDuration);
		}
	}

	private void TryUpdateDisplayStyle()
	{
		DisplayStyle displayStyle = DisplayStyle.normal;
		if (Game.currentGameMode == GameMode.NormalWildcard)
		{
			displayStyle = DisplayStyle.wildcard;
		}
		else if (displayedValue <= EconomySettings.lowLivesAmount)
		{
			displayStyle = DisplayStyle.lowValue;
		}
		if (currentDisplayStyle != displayStyle)
		{
			currentDisplayStyle = displayStyle;
			switch (currentDisplayStyle)
			{
			case DisplayStyle.normal:
				background.color = Color.white;
				label.color = Color.white;
				break;
			case DisplayStyle.wildcard:
				background.color = Globals.colorWildcard;
				label.color = new Color(32f / 85f, 0.8156863f, 25f / 51f);
				break;
			case DisplayStyle.lowValue:
				background.color = Color.white;
				label.color = new Color(1f, 0.2784314f, 0.2784314f);
				break;
			}
		}
	}

	public void SetSyncWithEconomy(bool state)
	{
		syncWithEconomy = state;
		if (syncWithEconomy)
		{
			value = Economy.lives;
		}
	}

	public void SyncWithEconomyOnce()
	{
		value = Economy.lives;
	}

	public void Add(int howMany)
	{
		value += howMany;
	}

	public float AddAnimated(int howMany, RectTransform from = null, float flowSize = 60f, bool syncAfter = false)
	{
		bool flag = false;
		bool flag2 = false;
		if (from != null)
		{
			flag = true;
			flag2 = true;
			animationFlow.animationEase = animationEase;
			animationFlow.animationInitialDelay = animationInitialDelay;
			animationFlow.animationMoveDuration = animationMoveDuration;
			animationFlow.animationInterval = animationInterval;
			AudioClip clip = null;
			if ((bool)Audio.GetInstance())
			{
				clip = Audio.GetInstance().soundLife;
			}
			animationFlow.Show(SpriteManager.GetInstance().spriteIconLife, from, icon.rectTransform, howMany, flowSize, clip, 0.5f);
		}
		Sequence sequence = DOTween.Sequence();
		float num = 0f;
		if (flag)
		{
			num += animationInitialDelay;
		}
		if (flag2)
		{
			num += animationMoveDuration;
		}
		if (num > 0f)
		{
			sequence.AppendInterval(num);
		}
		int initialDisplayValue = value;
		float duration = (float)Mathf.Clamp(howMany, 0, 10) * animationInterval - animationInterval;
		sequence.Append(DOTween.To(UpdateValue, initialDisplayValue, initialDisplayValue + howMany, duration));
		sequence.AppendCallback(delegate
		{
			UpdateValue(initialDisplayValue + howMany);
		});
		if (syncAfter)
		{
			sequence.AppendInterval(0.05f);
			sequence.AppendCallback(delegate
			{
				UserInterface.GetInstance().SyncDisplays();
			});
		}
		sequence.AppendCallback(delegate
		{
			GenericDialog.FireDialogFinishedUpdatingEvent();
		});
		sequence.Play();
		return GetAddAnimationDuration(howMany, flag, flag2);
	}

	public float GetAddAnimationDuration(int howMany, bool includeInitialDelay = true, bool includeMoveDuration = true)
	{
		float num = (float)Mathf.Clamp(howMany, 0, 10) * animationInterval - animationInterval;
		if (includeInitialDelay)
		{
			num += animationInitialDelay;
		}
		if (includeMoveDuration)
		{
			num += animationMoveDuration;
		}
		return num;
	}

	private void UpdateValue(float newValue)
	{
		value = Mathf.RoundToInt(newValue);
		TryUpdateDisplayValue();
	}

	private void ShowMinusOne()
	{
		if (sequenceMinusOne != null)
		{
			sequenceMinusOne.Kill();
		}
		imageMinusOne.rectTransform.anchoredPosition = new Vector2(-15f, -90f);
		imageMinusOne.rectTransform.localScale = Vector3.zero;
		imageMinusOne.color = Utils.SetAlpha(imageMinusOne.color, 0.7f);
		imageMinusOne.gameObject.SetActive(true);
		sequenceMinusOne = DOTween.Sequence();
		sequenceMinusOne.Append(imageMinusOne.rectTransform.DOScale(Vector3.one, 0.05f).SetEase(Ease.OutQuad));
		sequenceMinusOne.Append(DOTween.To(UpdateMinusOne, 0f, 1f, 0.6f).SetEase(Ease.Linear));
		sequenceMinusOne.AppendCallback(delegate
		{
			imageMinusOne.gameObject.SetActive(false);
		});
		sequenceMinusOne.Play();
	}

	public void StopMinusOne()
	{
		if (sequenceMinusOne != null)
		{
			sequenceMinusOne.Kill();
		}
		imageMinusOne.gameObject.SetActive(false);
	}

	private void UpdateMinusOne(float lerpValue)
	{
		imageMinusOne.rectTransform.anchoredPosition = Vector2.Lerp(new Vector2(-15f, -90f), new Vector2(-15f, -240f), lerpValue);
		imageMinusOne.color = Utils.SetAlpha(imageMinusOne.color, 1f - lerpValue);
	}
}
